package com.sl.biz.modular.problem.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sl.auth.core.util.StpCompositeUtil;
import com.sl.biz.modular.problem.dao.domain.entity.ProblemDO;
import com.sl.biz.modular.problem.dao.service.ProblemDaoService;
import com.sl.biz.modular.problem.service.ProblemBizService;
import com.sl.dev.api.DevFileApi;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ProblemBizServiceImpl implements ProblemBizService {

    private final ProblemDaoService problemDaoService;

    private final DevFileApi devFileApi;


    @Override
    public Boolean save(ProblemDO cdcConfigDO, MultipartFile file) {
        String id = cdcConfigDO.getId();
        if (StrUtil.isBlank(id)) {
            cdcConfigDO.setStatus(1);
        }
        String userId = StpCompositeUtil.getUserId();
        String userName = StpCompositeUtil.getUserName();
        String account = StpCompositeUtil.getAccount();

        cdcConfigDO.setReportTime(DateUtil.date());
        cdcConfigDO.setAccountNo(account);
        cdcConfigDO.setUserId(userId);
        cdcConfigDO.setUserName(userName);
        boolean save = problemDaoService.save(cdcConfigDO);

        if (ObjectUtil.isNull(file)) {
            return save;
        }
        //附件上传
        String s = devFileApi.uploadReturnId(file, cdcConfigDO.getId(), "PROBLEM");
        return save;
    }

    @Override
    public List<ProblemDO> list(ProblemDO problemDO) {
        return problemDaoService.list();
    }

}
